package com.base.cn.platform.os.controller.manage.config;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.service.manage.config.SysLinkConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 后台管理链接配置Controller
 *
 * @author s.li
 * @create 2018-04-10-18:58
 */
@Controller
@RequestMapping("/admin/manage/link")
public class AdminSysLinkConfigController extends AdminBaseController {

    @Autowired
    private SysLinkConfigService sysLinkConfigService;

    /**
     * 初始化链接配置管理页面
     * @return 操作页面
     */
    @RequestMapping("/initLinkConfigList")
    public String initLinkConfigList(){
        return resultPage("/manage/config/init-linkcogfig-list");
    }

    /**
     * 链接配置列表页面
     * @return 操作页面
     */
    @RequestMapping("/linkConfigList")
    public String linkConfigList(Model model,
                                 @RequestParam("linkType") String linkType){
        model.addAttribute("linkType",linkType);
        return resultPage("/manage/config/linkcogfig-list");
    }

    /**
     * 获取链接类型列表
     * @param model
     * @param params 查询条件
     * @return
     */
    @RequestMapping("/ajaxListConfigList")
    public String ajaxListConfigList(Model model,@RequestBody Map<String,Object> params){
        List<Map<String,Object>> linkConfigList = sysLinkConfigService.findSysLinkConfigList(params);
        model.addAttribute("linkConfigList",linkConfigList);
        return resultPage("/manage/config/ajax-linkconfig-list");
    }

    /**
     * 修改链接配置状态
     * @param ids 链接ID串
     * @param status 状态，1正常，2禁用，3删除
     * @return Map<String,Object>
     */
    @RequestMapping("/updateLinkConfigStatus")
    @ResponseBody
    public Map<String,Object> updateLinkConfigStatus(@RequestParam("ids") String ids,@RequestParam("status") int status){
       return sysLinkConfigService.updateSysLinkConfigStatus(ids,status);
    }

    /**
     * 初始化添加/修改链接配置页面
     * @param model  Model
     * @param parentId 父级ID
     * @param id 链接ID
     * @param linkType 类型
     * @return 操作页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "parentId",required = false,defaultValue = "0") BigDecimal parentId,
                                  @RequestParam(value = "id",required = false,defaultValue = "0") BigDecimal id,
                                  @RequestParam(value = "linkType",required = false) String linkType){
        Map<String,Object> linkConfig = null;
        if(DataUtil.idIsNotNull(id)){
            linkConfig = sysLinkConfigService.findSysLinkConfigById(id);
        }
        if(ObjectUtils.isEmpty(linkConfig)){
            linkConfig = new HashMap<>();
            linkConfig.put("linkType",linkType);
            linkConfig.put("parentId",parentId);
        }
        model.addAttribute("linkConfig",linkConfig);
        return resultPage("/manage/config/save-linkconfig");
    }

    /**
     * 保存链接配置数据
     * @param params 保存参数
     * @return Map<String,Object>
     */
    @RequestMapping("/saveLinkConfig")
    @ResponseBody
    public Map<String,Object> saveLinkConfig(@RequestBody Map<String,Object> params){
        return sysLinkConfigService.saveSysLinkConfig(params);
    }
}
